// pages/evaluating/result/result.js
const App = getApp()
// let AJAX = require("../../../utils/util.js").AJAX
// let timeTranfer = require("../../../utils/util.js").timeTranfer
let { AJAX, timeTranfer,imgUrl } = require("../../../utils/util.js")
import * as echarts from '../../../utils/ec-canvas/echarts';
const app = getApp();
Page({
   /**
    * 页面的初始数据
    */
   data: {
      navH: '',
      curIndex: 0,
      list: [],
      ec: {
         lazyLoad: true // 延迟加载
      },
      question: [],
      winHeight: "", //窗口高度
      currentTab: 0, //预设当前项的值
      scrollLeft: 0, //tab标题的滚动条位置
     imgUrl: imgUrl
   },

   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function(options) {
      this.setData({

         navH: App.globalData.navHeight,

      })
      this.echartsComponnet = this.selectComponent('#mychart-dom-bar')
      this.requestResult()
      this.requestQuestio()
      var that = this;
      //  高度自适应
      wx.getSystemInfo({
         success: function(res) {
            var clientHeight = res.windowHeight,
               clientWidth = res.windowWidth,
               rpxR = 750 / clientWidth;
            var calc = clientHeight * rpxR - 180;
            console.log(calc)
            that.setData({
               winHeight: calc
            });
         }
      });
   },

   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function() {

   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function() {

   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function() {

   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function() {

   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function() {

   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function() {

   },

   /**
    * 用户点击右上角分享
    */
   onShareAppMessage: function() {

   },
   // 跳转
   navBack(){
      wx:wx.navigateTo({
         url: '/pages/evaluating/evaluating'         
      })
   },
   // 导航栏点击事件
   navClickEvent(e) {
      let _i = e.currentTarget.dataset.index
      this.setData({
         curIndex: _i
      })
   },
   // 获取测试结果
   requestResult() {
      AJAX("question-guide/result").then(res => {
         this.setData({
            list: res.data
         })

         this.initChart(); //初始化图表

      })
   },
   /**
    * 维度
    * questio
    */
   requestQuestio() {
      AJAX("question-guide/type").then(res => {
         this.setData({
            question: res.data
         })
         console.log(res.data)

      })
   },
   // 导航
   // 滚动切换标签样式
   switchTab: function(e) {
      this.setData({
         currentTab: e.detail.current
      });
      this.checkCor();
   },
   // 点击标题切换当前页时改变样式
   swichNav: function(e) {
      var cur = e.currentTarget.dataset.current;
      // var cur = event.currentTarget.dataset.current;
      // //每个tab选项宽度占1/5
      // var singleNavWidth = this.data.windowWidth / 5;
      if (this.data.currentTaB == cur) {
         return false;
      } else {
         this.setData({
            currentTab: cur
         })
      }
   },
   //判断当前滚动超过一屏时，设置tab标题滚动条。
   checkCor: function() {

      console.log(this.data.currentTab)
      if (this.data.currentTab >= 3) {

         this.setData({
            scrollLeft: 300
         })
      } else {
         this.setData({
            scrollLeft: 0
         })
      }
   },

   // canvas
   initChart: function() {
      this.echartsComponnet.init((canvas, width, height) => {
         // 初始化图表
         const Chart = echarts.init(canvas, null, {
            width: width,
            height: height
         });
         //this.data.list
         Chart.setOption(this.getOption(this.data.list));
         // 注意这里一定要返回 chart 实例，否则会影响事件处理等
         return Chart;
      })
   },
   getOption: function(list) {
      // 指定图表的配置项和数据
      console.log(list)
      var option = {
         backgroundColor: "#ffffff",
         color: ["#37A2DA", "#FF9F7F"],
         tooltip: {},
         xAxis: {
            show: false
         },
         yAxis: {
            show: false
         },
         radar: {
            // shape: 'circle',
            indicator: [{
                  name: '食品',
                  max: 12
               },
               {
                  name: '玩具',
                  max: 12
               },
               {
                  name: '服饰',
                  max: 12
               },
               {
                  name: '绘本',
                  max: 12
               },
               {
                  name: '医疗',
                  max: 12
               },
               {
                  name: '门票',
                  max: 12
               },
               {
                  name: '医疗',
                  max: 12
               },
               {
                  name: '门票',
                  max: 12
               }
            ]
         },
         series: [{
            name: '预算',
            type: 'radar',
            data: [{
               value: list.store,
               name: '预算'
            }]
         }]
      }
     option.radar.indicator = list.result

      return option;
   }


})